<template>
	<view class="base-info uni-padding-wrap">
		<view class="form-group avatar-wrap" @tap="changeValue" data-element="image" :data-value="info.avatar" data-page_title="设置头像">
			<text class="field-name">头像</text>
			<image :src="info.avatar" class="avatar" mode="aspectFill"></image>
			<icon class="iconfont icon-right" />
		</view>
		<view class="form-group name-wrap" @tap="changeValue" data-element="input" :data-value="info.name" data-page_title="设置用户名" data-required="1">
			<text class="field-name">用户名</text>
			<text class="name">{{ info.name }}</text>
			<icon class="iconfont icon-right" />
		</view>
		<view class="form-group" @tap="doSave">
			<button type="primary" class="submit">保存</button>
		</view>
	</view>
</template>

<script>
	import ListOptions from '../../minxins/list-options.js'

	export default {
		data() {
			return {
				page: 'base-info',
				info: {}
			}
		},
		mixins: [ListOptions],
		components: {

		},
		async onLoad(options) {
			this.options = Object.assign({}, this.options, options)
			try {
				let result = await this.fetchPageData()
				this.renderPage(result)
			} catch (error) {

			}
		},
		onShow() {
			let newName = this.$store.getters.getNewName;
			console.log(`onShow, newName: `, newName)
			if (newName !== null) {
				this.info.name = newName
			}
			let newAvatar = this.$store.getters.getNewAvatar;
			console.log(`onShow, newAvatar: `, newAvatar)
			if (newAvatar !== null) {
				this.info.avatar = newAvatar
			}
		},
		methods: {
			async fetchPageData() {
				let user = this.$getUser()
				if (!user.key) {
					console.log(`no user info...`)
					return
				}
				this.options.__resource_id = user.key
				let result = await this.$api.my(this.options);
				return result;
			},
			changeValue(e) {
				console.log(e);
				let dataset = e.currentTarget.dataset
				if (dataset.element) {
					wx.navigateTo({
						url: "/pages/form-group/" + dataset.element + "?value=" + dataset.value + "&page_title=" + dataset.page_title + "&required=" + dataset.required
					})
				}
			},
			renderPage(result) {
				if (result.ret == 0) {
					this.info = result.data
				}
			},
			async doSave(e) {
				console.log('doSave', e, this.info)
				let user = this.$getUser()
				if (!user.key) {
					console.log('no user info...')
					return
				}
				let params = this.info
				params.__resource_id = user.key
				let result = await this.$api.updateUser(params)
				if (result.ret == 0) {
					this.$store.commit('setBackToMyReferer', 'pages/my/base-info')
					uni.navigateBack({
						delta: 1
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.base-info {
		.form-group {
			&:not(:last-child) {
				border-bottom: 1rpx solid #eee;
			}

			padding: 20rpx 0;
			display: flex;
			align-items: center;

			.field-name {
				flex-grow: 1;
			}

			.iconfont {
				margin-left: 10rpx;
			}
		}

		.avatar-wrap {
			.avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 100%;
			}
		}

		.submit {
			width: 100%;
		}
	}
</style>
